<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script type="text/javascript"  src="http://192.168.101.2:8081/statics/map/common/jquery.min.js"></script>
<script type="text/javascript" src="http://192.168.101.2:8081/statics/map/js/init.js"></script> 
<style type="text/css">
html, body {
	width: 100%;
	height: 100%;
	overflow: hidden;
	margin: 0;
	padding: 0;
	font-size:14px;
}
 #Map-buttons {
	width: 200px;
	position: fixed;
	background-color: #ffffff;
	top: 0px;
	padding:10px;
	margin:20px;
	box-shadow: 5px 5px 3px #aaa;
	border:solid 1px #aaa;
	border-radius:5px;
}

#drawArea{width:80px;}
#drawPoint{width:80px;}
#drawReturn{width:51px;}
#drawConfirm{width:51px;}
.easyui-linkbutton {
 float:left;
 margin-right:4px;
	border:solid 1px #666;
	text-align:center;
	height:30px;
	border-radius:5px;
	background-color:#337ab7;
	color:#fff;
	text-decoration:none;
	line-height:30px;
} 
</style>
<script>
	var map;
	var geom;
	var newgeom;
	var detype = 'point';
	var host_url = 'http://192.168.101.2:8081/'; 
	$(document).ready(function() { 
		detype = getQueryString('type');
		initMap(); 
     });
    	 
	//获取url参数
	function getQueryString(name) {//no
		var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
		if (reg.test(location.href))
			return unescape(RegExp.$2.replace(/\+/g, " "));
		return "";
	}
	function initMap() {
		map = new EJMap("mapcontent", {layers:[{id:"管线",display:false,visible:false },{id:"区域",display:false,visible:false}]
});
		map.addEventListener(EJMap.Event.ondeactivate, function(e) {
			newgeom = e.geometry.toJson();
			var type = getType(newgeom);
			geomDraw(type)
		});
		map.addEventListener(EJMap.Event.onMapLoaded, function(e) {
			addGisInfo();
		});
	}
	function getType(geom) {//no
		if (!!!geom) {
			return detype;
		}
		if (!!geom.x && !!geom.y) {
			return "point";
		} else if (!!geom.rings) {
			return "polygon";
		} else if (!!geom.paths) {
			return "polyline";
		} else {
			return detype;
		}
	}

	function addGisInfo() {
		if (window.opener) {
			var type = getType(newgeom);
			if (type == 'point') {
				$(".set").hide();
				$("#drawPoint").show();
				$("#drawReturn").show();
				$("#drawConfirm").show();
					if(!!window.opener.vm.orderX&&!!window.opener.vm.orderY){
						geom = newgeom = {
								x : window.opener.vm.orderX,
								y : window.opener.vm.orderY,
								spatialReference : {
									wkid : 4326
								}
					}			
				};
			} else if (type = 'polygon') {
				$("#drawArea").show();
				$("#drawReturn").show();
				$("#drawConfirm").show();
				if(!!window.opener.vm.region.geomString){
					geom = newgeom = eval('('+window.opener.vm.region.geomString+')');
				}			
			}
			if(!!newgeom){
				geomDraw(type);
			}
		}
	}
	
	function addGisInfo2() {
		if (window.opener) {
			var type = getType(newgeom);
			if(!!newgeom){
				geomDraw(type);
			}
		}
	}
	
	
	function geomDraw(type) {
		switch (type) {
		//画点	
		case "point":
			$(".set").hide();
			if (!!newgeom) {
				funCallbackPoint(newgeom);
			}
			;
		
			break;
		case "polygon":
			if (!!newgeom) {
				funCallbackPolygon(newgeom);
			}
		
			break;
		}
	}
	function funCallbackPoint(e) {
		newgeom = {
			x : e.x,
			y : e.y,
			spatialReference : {
				wkid : 4326
			}
		};
		map.addPoint2GraphicsLayer({
			geom : newgeom,
			symbol : {
				"url" : host_url + "userConfig/symbols/pic_blue.png",
				"height" : 15,
				"width" : 15,
				"type" : "esriPMS"
			},
			edit : true,
			center:true
		}, "layer_", true);
		
	}  
	//点
	function getCoordPoint() {
		map.getPoint(funCallbackPoint);
	}

	//编辑重绘
	function funReDrawPolygon(json) {
		newgeom = "";
		map.clearLayerById("layer_");
	}
	function returnArea() {
		addGisInfo();
	}
	function confirmArea() {
		var type = getType(newgeom);
		if (window.opener && window.opener.vm != null) { 
			window.opener.vm.setGeomString(newgeom); 
			window.close();
		}else{
			console.log(window.opener);
			console.log(window.opener.vm);
		}
	}
	window.onbeforeunload = function(event) {
		confirmArea();
	};
</script>
</head>
<body>
	<div id="mapcontent" style="width: 100%; height: 100%">
		<noscript>加载地图失败</noscript>
	</div>
	<div id="Map-buttons">  
		<a href="#" class="easyui-linkbutton" style="display: block" id="drawPoint" onclick="getCoordPoint()">设置点</a>
		<a href="#" class="easyui-linkbutton" style="display: block" id="drawReturn" onclick="returnArea()">还原</a>
		<a href="#"	class="easyui-linkbutton" style="display: block" id="drawConfirm" onclick="confirmArea()">确认</a>
	</div>
</body>

</html>
